package com.yzy.soft.system.controller;


import com.yzy.soft.core.base.controller.BaseController;
import com.yzy.soft.core.util.FileUtil;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yinzhiyuan
 * @since 2018-02-01
 */
@RestController
@RequestMapping("/file")
public class FileController extends BaseController {
//    @Autowired
//    private AccountconditioncodeMapper accountconditioncodeMapper;
//@RequestParam(name = "page", defaultValue = "1", required = true) int page, @RequestParam(name = "limit", defaultValue = "10", required = true) int pageSize

    @GetMapping("")
    public String getAll() {
        String path="D://Controller.ftl";
        byte[]  s= FileUtil.toByteArray(path);
        String sf="";
        try{
            sf=new String(s,"UTF-8");
        }catch(UnsupportedEncodingException e){

        }

        return sf;
    }

    @RequestMapping(value = "/export1")
    public ResponseEntity<FileSystemResource> listExport() {
//        File file = orderService.listExport(proNo);
        File file = new File("D://Controller.ftl");
        return export(file,String.valueOf(System.currentTimeMillis()),"ftl");
    }

    @RequestMapping(value = "/export")
    public ResponseEntity<byte[]> eExport() {
//        File file = orderService.listExport(proNo);
        File file = new File("D://Controller.ftl");
        return renderFile(String.valueOf(System.currentTimeMillis())+".ftl","D://Controller.ftl");
    }
    @GetMapping("/download/zip")
    public void downloadZipFile(HttpServletResponse response) throws IOException {

        response.setContentType(MediaType.APPLICATION_OCTET_STREAM.toString());
        response.setHeader("Content-Disposition","attachment; filename=\"images.zip\"");

        List<String> fileNames = Arrays.asList("D://Controller.ftl");
        ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());
String fileName1="Controller.ftl";

        for(String fileName : fileNames) {
            ZipEntry zipEntry = new ZipEntry(fileName1);
            zipOutputStream.putNextEntry(zipEntry);
            FileInputStream inputStream = new FileInputStream(fileName);
            IOUtils.copy(inputStream,zipOutputStream);
            inputStream.close();
        }

        zipOutputStream.closeEntry();
        zipOutputStream.close();
    }

    public static void main(String[] args){
//String path="D://Controller.ftl";
//        byte[]  s= FileUtil.toByteArray(path);
System.out.println(new FileController().getAll());

    }
}

